<template>
	<view>
		<shop-detail :commodityData="commodity"></shop-detail>
		<shopBuy :isCollected="is_Collected" @collectClick="collect" @singleBuyClick="test2" @groupBuyClick="test3">
		</shopBuy>
	</view>
</template>

<script>
	import shopDetail from '@/components/shop-detail.vue'
	import shopBuy from '@/components/shop-buy.vue'
	export default {
		components: {
			shopDetail,
			shopBuy
		},
		data() {
			return {
				// 首页点击的商品 货物
				commodity: {},
				is_Collected: false
			}
		},
		methods: {
			/**
			 * 发送请求加载  指定商品的详情
			 */
			getShopDetail(commodityId) {
				let that = this

				uni.request({
					url: 'http://127.0.0.1:8002/detail/?id=' + commodityId,
					method: "GET",
					success(res) {
						let dataAll = res.data.data[0]
						let detail = res.data.data[0].fields.detail
						// 使用正则表达式替换src字段的内容
						detail = detail.replace(/src="(.+?)"/g, 'src="http://127.0.0.1:8002$1"');
						dataAll.fields.detail = detail
						that.commodity = dataAll
					}
				})
			},
			collect() {
				console.log("点击收藏")
				this.checkOrLogin()
			},
			test2() {
				console.log("2")
			},
			test3() {
				console.log("3")
			},
			checkOrLogin() {
				// 检查用户是否已登录
				var loginUser = uni.getStorageSync('loginUser');
				if (!loginUser) {
					var pages = getCurrentPages();
					uni.setStorageSync('current-path', pages[0].$page.path);
					uni.redirectTo({
						url: '/pages/login/login'
					});
				} else {
					this.JudgmentIscollected(loginUser);
				}
			},
			JudgmentIscollected(loginUser) {
				uni.showLoading({
					title: '执行中...',
					mask: true
				});
				const that = this;
				uni.request({
					url: `http://127.0.0.1:8002/isCollect?username=${loginUser.username}&spu=${that.commodity.pk}`,
					method: 'GET',
					success(res) {
						if (res.data.code == 2000) {
							that.is_Collected = res.data.data;
							console.log(that.is_Collected);
							that.handleCollect(loginUser);
						}
					},
					fail() {
						uni.hideLoading();
					}
				});
			},
			handleCollect(loginUser) {
				const that = this;
				/**
				 * 如果已经收藏就取消收藏，反之就收藏
				 */
				const url = that.is_Collected ?
					`http://127.0.0.1:8002/CancelCollect?username=${loginUser.username}&spu=${that.commodity.pk}` :
					`http://127.0.0.1:8002/collect?username=${loginUser.username}&spu=${that.commodity.pk}`;

				uni.request({
					url: url,
					method: 'GET',
					success(res) {
						if (res.data.code == 2000) {
							console.log(res.data);
							if (res.data.status == 'success') {
								that.is_Collected = !that.is_Collected;
								uni.hideLoading();
								uni.showToast({
									title: res.data.message,
									icon: res.data.status,
									duration: 2000,
									mask: true
								});
							}
						}
					},
					fail() {
						uni.hideLoading();
					}
				});
			}
		},
		onLoad: function(option) {
			console.log(option)
			this.getShopDetail(option.id)

			const that = this
			var loginUser = uni.getStorageSync('loginUser');
			uni.request({ // 判断该商品是否被该用户收藏
				url: `http://127.0.0.1:8002/isCollect?username=${loginUser.username}&spu=${option.id}`,
				method: 'GET',
				success(res) {
					that.is_Collected = res.data.data
				}
			})
		}
	}
</script>

<style>

</style>
